package com.example.demo;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;
    private MyButton btn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn5,"alpha",1,0,1);
//                objectAnimator.setDuration(3000);
//                objectAnimator.setRepeatCount(3);
//                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
//                objectAnimator.start();
                //第二种
//                Animator animator = AnimatorInflater.loadAnimator(MainActivity.this, R.animator.set_animation2);
//                animator.setTarget(btn5);
//                animator.start();

                //组合动画
                //1.  设置需要组合的动画效果
                float curTranslationX = btn5.getTranslationX();
                //平移
                ObjectAnimator translation = ObjectAnimator.ofFloat
                        (btn5,"translationX",curTranslationX,300,curTranslationX);
                //旋转
                ObjectAnimator rotate = ObjectAnimator.ofFloat(btn5,"rotation",0f,360f);
                //透明度
                ObjectAnimator alpha = ObjectAnimator.ofFloat(btn5,"alpha",1f,0f,1f);

                //2  创建组合动画的对象
                AnimatorSet animatorSet = new AnimatorSet();

                //3 根据需求组合动画
                animatorSet.play(translation).with(rotate).before(alpha);
                animatorSet.setDuration(5000);
                //4 启动
                animatorSet.start();

            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float curTranslationX = btn5.getTranslationX();
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat
                        (btn5,"translationX",curTranslationX,600,curTranslationX);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value = (float) animation.getAnimatedValue();
                        btn5.setText("Hello"+value);

                    }
                });
                objectAnimator.start();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn5,"scaleX",1,3,1);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn5,"rotation",0f,360f);
                objectAnimator.setDuration(3000);
                objectAnimator.setRepeatCount(3);
                objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
                objectAnimator.start();
            }
        });


    }
}
